package com.ruoyi.web.controller.system;

import cn.hutool.core.lang.Pair;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.impl.SysRegisterSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController {
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;
    @Resource
    private SysRegisterSupport sysRegisterSupport;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user) {
       /* if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }*/
        Pair<String, SysUser> pair = registerService.register(user);
        //如果有返回错误信息，则注册失败,不执行扩展操作
        if (StringUtils.isNotEmpty(pair.getKey())) {
            return error(pair.getKey());
        }
        //注册成功后为每个人创建默认分组
        sysRegisterSupport.registerExtends(pair.getValue());

        return StringUtils.isEmpty(pair.getKey()) ? success() : error(pair.getKey());
    }
}
